package com.xingxing.Fragment;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.OrientationHelper;
import androidx.recyclerview.widget.RecyclerView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xingxing.Activity.R;
import com.xingxing.Activity.databinding.FragmentHomeBinding;
import com.xingxing.Adapter.DikDokAdapter;
import com.xingxing.Bean.Video;
import com.xingxing.Interface.OkSuccessInterface;
import com.xingxing.Manager.CustomLayoutManager;
import com.xingxing.Manager.OnPageSlideListener;
import com.xingxing.Util.CommonUtils;
import com.xingxing.Util.APIConfig;
import com.xingxing.View.CusVideoView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import static com.xingxing.Util.CommonUtils.GetUserInfo;
import static com.xingxing.Util.CommonUtils.OKJsonPost;

public class HomeFragment extends Fragment {
    private FragmentHomeBinding binding;
    private RecyclerView mRecycler;
    private CustomLayoutManager mLayoutManager;
    ArrayList<Video> videos;
    boolean isLike = false;

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentHomeBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
        initView();
        initListener();
        return root;
    }

    //初始化View
    private void initView() {
        mRecycler = binding.mRecycler;
        mLayoutManager = new CustomLayoutManager(getContext(), OrientationHelper.VERTICAL, false);
        Map<String, Object> map = new HashMap<>();
        map.put("type", "all");
        CommonUtils.OKJsonPost(getActivity(), new Gson().toJson(map), APIConfig.GetVideo, new OkSuccessInterface() {
            @Override
            public void OnSuccess(String json) {
                videos = new Gson().fromJson(json, new TypeToken<ArrayList<Video>>() {
                }.getType());
                DikDokAdapter mAdapter = new DikDokAdapter(getContext(), json);
                mRecycler.setLayoutManager(mLayoutManager);
                mRecycler.setAdapter(mAdapter);
            }
        });
    }

    //初始化监听
    private void initListener() {
        mLayoutManager.setOnPageSlideListener(new OnPageSlideListener() {
            @Override
            public void onPageRelease(boolean isNext, int position) {
                int index;
                if (isNext) {
                    index = 0;
                } else {
                    index = 1;
                }
                releaseVideo(index);
            }

            @Override
            public void onPageSelected(int position, boolean isNext) {
                playVideo(position);
            }
        });
    }

    //播放
    private void playVideo(int position) {
        View itemView = mRecycler.getChildAt(0);
        final CusVideoView mVideoView = itemView.findViewById(R.id.mVideoView);
        final ImageView mPlay = itemView.findViewById(R.id.mPlay);
        final ImageView mThumb = itemView.findViewById(R.id.mThumb);
        final TextView like = itemView.findViewById(R.id.like);
        final MediaPlayer[] mMediaPlayer = new MediaPlayer[1];
        mVideoView.start();
        Map<String, Object> map = new HashMap<>();
        map.put("type", "getCount");
        map.put("userAccount", Objects.requireNonNull(GetUserInfo()).getUserAccount());
        map.put("videoID", videos.get(position % videos.size()).getVideoID());
        OKJsonPost(getActivity(), new Gson().toJson(map), APIConfig.LikeVideo, new OkSuccessInterface() {
            @Override
            public void OnSuccess(String json) {
                Map<String, Object> map = new HashMap<>();
                map = new Gson().fromJson(json, map.getClass());
                like.setText((String) map.get("count"));
            }
        });

        mVideoView.setOnInfoListener(new MediaPlayer.OnInfoListener() {
            @Override
            public boolean onInfo(MediaPlayer mp, int what, int extra) {
                mMediaPlayer[0] = mp;
                mp.setLooping(true);
                mThumb.animate().alpha(0).setDuration(200).start();
                return false;
            }
        });

        //暂停控制
        mPlay.setOnClickListener(new View.OnClickListener() {
            boolean isPlaying = true;
            @Override
            public void onClick(View v) {
                if (mVideoView.isPlaying()) {
                    mPlay.animate().alpha(1f).start();
                    mVideoView.pause();
                    isPlaying = false;
                } else {
                    mPlay.animate().alpha(0f).start();
                    mVideoView.start();
                    isPlaying = true;
                }
            }
        });

        like.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Map<String, Object> map = new HashMap<>();
                map.put("type", "like");
                map.put("userAccount", Objects.requireNonNull(GetUserInfo()).getUserAccount());
                map.put("videoID", videos.get(position % videos.size()).getVideoID());
                OKJsonPost(getActivity(), new Gson().toJson(map), APIConfig.LikeVideo, new OkSuccessInterface() {
                    @Override
                    public void OnSuccess(String json) {
                        Map<String, Object> map = new HashMap<>();
                        map = new Gson().fromJson(json, map.getClass());
                        if (map.get("status").equals("成功")) {
                            like.setText((String) map.get("count"));
                        }
                    }
                });
            }
        });
    }

    //释放
    private void releaseVideo(int index) {
        View itemView = mRecycler.getChildAt(index);
        final CusVideoView mVideoView = itemView.findViewById(R.id.mVideoView);
        final ImageView mThumb = itemView.findViewById(R.id.mThumb);
        final ImageView mPlay = itemView.findViewById(R.id.mPlay);
        mVideoView.stopPlayback();
        mThumb.animate().alpha(1).start();
        mPlay.animate().alpha(0f).start();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}
